# Copyright (c) 2021, yuxinyong and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document
from erpnext.manufacturing.doctype.work_order.work_order import get_item_details
import json

class PlanOrder(Document):
	pass


# @frappe.whitelist()
# def raise_work_orders(material_request):
# 	mr= frappe.get_doc("Material Request", material_request)
# 	errors =[]
# 	work_orders = []
# 	default_wip_warehouse = frappe.db.get_single_value("Manufacturing Settings", "default_wip_warehouse")

# 	for d in mr.items:
		

# 			work_orders.append(wo_order.name)

# 	if work_orders:
# 		work_orders_list = [get_link_to_form("Work Order", d) for d in work_orders]

# 		if len(work_orders) > 1:
# 			msgprint(_("The following {0} were created: {1}")
# 				.format(frappe.bold(_("Work Orders")), '<br>' + ', '.join(work_orders_list)))
# 		else:
# 			msgprint(_("The {0} {1} created sucessfully")
# 				.format(frappe.bold(_("Work Order")), work_orders_list[0]))

# 	if errors:
# 		frappe.throw(_("Work Order cannot be created for following reason: <br> {0}")
# 			.format(new_line_sep(errors)))

# 	return work_orders

@frappe.whitelist()
def create_work_order(plan_order, default_wip_warehouse = None, bulk_mode = False):
	if isinstance(plan_order, str):
		plan_order = json.loads(plan_order)
	if not plan_order: return
	plan_order = frappe._dict(plan_order)	
	if not default_wip_warehouse:
		default_wip_warehouse = frappe.db.get_single_value("Manufacturing Settings", "default_wip_warehouse")
	bom_no = plan_order.bom or get_item_details(plan_order.item_code).bom_no	
	if bom_no:
		wo_order = frappe.new_doc("Work Order")
		wo_order.update({
			"production_item": plan_order.item_code,
			"qty": plan_order.qty,
			"fg_warehouse": plan_order.warehouse,
			"wip_warehouse": default_wip_warehouse,
			"description": plan_order.item_name,
			"stock_uom": plan_order.stock_uom,
			"expected_delivery_date": plan_order.due_date,
			"sales_order": plan_order.ref_doctype == "Sales Order" and plan_order.ref_docname or None,
			"sales_order_item":plan_order.ref_doctype == "Sales Order" and plan_order.ref_doc_item or None,
			"bom_no": bom_no,
			"planned_start_date": plan_order.order_release_date,
			"company": plan_order.company
		})
		wo_order.set_work_order_operations()
		if bulk_mode:
			wo_order.save()
		return wo_order.as_dict()

@frappe.whitelist()
def create_purchase_order(plan_order, bulk_mode = False):
	if isinstance(plan_order, str):
		plan_order = json.loads(plan_order)
	if not plan_order: return
	plan_order = frappe._dict(plan_order)	

	po_order = frappe.new_doc("Purchase Order")
	po_order.update({
		"supplier": plan_order.supplier,
		"schedule_date": plan_order.due_date,
		"company": plan_order.company
	})
	po_order.append('items',{
		"item_code": plan_order.item_code,
		"item_name": plan_order.item_name,
		"qty": plan_order.qty,
		"stock_uom": plan_order.stock_uom
	})
	if bulk_mode:
		po_order.save()
	#po_order.name = 'new-purchase-order-1'
	return po_order.as_dict()		